#!/bin/bash
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#::                                                                            ::
#::                                                                            ::
#:: Program name : VERSION                                                     ::
#::                                                                            ::
#:: Version      : 6.00                                                        ::
#::                                                                            ::
#:: Copyright    : IBM Confidential                                            ::
#::                                                                            ::
#::                5724-B16 (C) Copyright IBM Corp. 2000, 2002                 ::
#::                                                                            ::
#::                The source code for this program is not published           ::
#::                or otherwise divested of its trade secrets,                 ::
#::                irrespective of what has been deposited with                ::
#::                the U.S. Copyright Office.                                  ::
#::                                                                            ::
#:: Contents     : PD Information Packager                                     ::
#::                                                                            ::
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


#
# print_copyright
#     This function prints out the correct copyright statement 
#
print_copyright ()
{
    echo
    echo "IBM (TM) LAN Distributed Platform Version 6.0 - LANDP"
    echo "5724-B16 (C) Copyright IBM Corp. 2000, 2002"
    echo "Licensed Materials - Property of IBM - All rights reserved"
    echo "(TM) Trademark of International Business Machines Cororation"
    echo "LANDP is a trademark of International Business Machines Corporation"
    echo "-------------------------------------------------------------------"
}


#
# print_usage
#     This function prints out the available options for version
#
print_usage ()
{
    echo "LANDP Version Utility"
    echo "---------------------"
    echo
    echo "usage:  version [options]"
    echo 
    echo "Options:"
    echo "    -o file_name     Send output to file_name"
}

#
# get_parameters
#     This function parses the input parameter list 
#     and sets the necessary variables.
# 
get_parameters ()
{
    while [ -n "$1" ]
    do
       case $1 in
          -o)            shift; output=$1;;
          --help)        print_usage; exit ;;
          *)             print_usage; exit ;;
       esac
       shift
    done

}


#
# init_version
#     This function controls the initialisation of version. 
#     The function opens SERVERS.LST for processing and creates        
#     the necessary output file.
#     
init_version ()
{
    output="version.txt"
    servList="servers.lst"
    uservList="userserv.lst"
    
    get_parameters $@
    print_copyright 
    
    # Check that servers.lst exists in the current working directory. 
    if [ ! -e $servList ]
    then
        echo "Error: servers.lst and/or userserv.lst not found in current working directory" exit 
    fi

    # Check that servers.lst exists in the current working directory. 
    if [ ! -e $uservList ]
    then
        uservList=""
    fi
    
    # Create Output File
    echo "LANDP Version Utility " `date` > $output
}
   
   
#
# This function processes both the servers.lst and userserv.lst
# (if it exists) files.  It determines which version would be 
# run using the Linux which command.  The for each directory   
# the function version is run for each directory accumulated.
#
get_runtime_information ()
{
   # read each server entry in the servers.lst file
   # Each entry is made up of 'SERVER' and a ID
   while read servInfo
   do
      # Extract the server and the ID parts
      server=${servInfo% *}
      ident=${servInfo##* }
      
      # Find out which executable would be run
      exe=`/usr/bin/which $server 2>&1`
      if [ $? = 0 ] 
      then
         echo $server  >> $output
         echo "------------------" >> $output
         echo "Actual Executable Location :" $exe >> $output
         
         # Extract The Version level
         echo "Program Information         :" `ehcvlext -e $exe -i $ident -c` >> $output
         
         # Get library link information
         echo "Library Dependencies :"  >> $output
         ldd $exe >> $output
      
         echo "=================================================================================" >> $output 
      fi
            
   done < $servList
   
            
   echo                    >> $output
   echo                    >> $output
   echo "User Servers : "  >> $output
   echo                    >> $output
   
   if [ "$uservList" != "" ] 
   then     
      # read each server entry in the servers.lst file
      # Each entry is made up of 'SERVER' and a ID
      while read servInfo
      do
         # Extract the server and the ID parts
         server=${servInfo% *}
         ident=${servInfo##* }
         
         # Find out which executable would be run
         exe=`/usr/bin/which $server 2>&1`
         if [ $? = 0 ] 
         then
            echo $server  >> $output
            echo "------------------" >> $output
            echo "Actual Executable Location :" $exe >> $output
            
            # Extract The Version level
            echo "Program Information         :" `ehcvlext -e $exe -i $ident -c` >> $output
            
            # Get library link information
            echo "Library Dependencies :"  >> $output
            ldd $exe >> $output
         
            echo "=================================================================================" >> $output 
         fi
               
      done < $uservList
   fi
         
}


# The program                                                
init_version $@
get_runtime_information
